/*
 * CS_BLACKSMITH_UPSTONE_LIST.cpp
 *
 *  Created on: 2013. 5. 28.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NResource/Static.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_BLACKSMITH_UPSTONE_LIST(User * pUser, ::Network::Packet * pPacket)
{
/*	대장간 정보 요청
	ZNO_CS_BLACKSMITH_UPSTONE_LIST //0x2200
	{

	}

	대장간 정보 요청 응답
	ZNO_SC_BLACKSMITH_UPSTONE_LIST //0x2201
	{
		u16	u16ack				// 1:OK -1:오류
		u8 	u8count
		{
			u64	upstone_seq;	// 강화 시퀀스
			u8 	u8upstone_state; 	// 진행중 : 0, 완료 : 1
			u8	u8rank;			// 등급 (0: F, 1: E, 2:D, 3:C, 4:B, 5:A, 6:S, 7:SS, 8:SSS)
			u16	u16item_level;		// 완료시 아이템 레벨
			u32	u32upstone_time;	// 남은 시간
			u32	u32upstone_max_time;	// 원래 시간
			u16	u16need_zen;		// 필요 젠( 즉시 완료시 )
		}
	}*/

	PREPARE_PACKET;

	NResource::BalanceData * pBalance = NResource::Static::InstancePtr()->GetBalance()->GetBalancePtr(pUser);
	if (NULL == pBalance)
	{
		DEBUGLOG("CS_BLACKSMITH_UPSTONE_LIST : pBalance is NULL!");
		return;
	}

	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::S_PROCEDURE);
	clsAuto->MakeQuery("call sp_select_data_upstone_list(%d)", pUser->GetCharacterInfo().characterseq);

	if (false == ::Database::GETSESSION()->SyncExecute(pUser->GetUserInfo().shard_Item_db_type, clsAuto))
	{
		DEBUGLOG("CS_BLACKSMITH_UPSTONE_LIST : false == ::Database::GETSESSION()->SyncExecute")
		return;
	}

	if (clsAuto->GetErrorIdx() != 0)
	{
		DEBUGLOG("CS_BLACKSMITH_UPSTONE_LIST : clsAuto->GetErrorIdx() != 0")
		return;
	}

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();

//	*pAck << static_cast<uint16_t>(ACK_OK);
	*pAck << static_cast<char>(clsAuto->ResultRowCount());

	uint64_t	seq			= 0;
	uint8_t		status		= 0;
	uint8_t		quality		= 0;
	int32_t		remainSec	= 0;
	int32_t		totalSec	= 0;
	uint16_t	needZen		= 0;
	uint16_t	stoneLevel	= 0;

	while (clsAuto->Next())
	{
		clsAuto->GetRecord() >> seq >> status >> quality >> needZen >> stoneLevel >> remainSec >> totalSec;

		// 강화석 제작 시간이 지난 경우 (??초 차이는 완료로 판단)
		// 상태를 '완료'로 바꾸고, 남은 시간은 0으로 설정
		if (remainSec <= pBalance->GetValue(EM_UPSTONE_COMBINE_COMPLETE_ADMIT_SEC))
		{
			status		= 1;
			remainSec	= 0;
		}

		// 강화석 제작 목록 설정
		*pAck << seq << status << quality << stoneLevel << remainSec << totalSec << needZen;
	}

	pAck->MakePacket(CMD_SC_BLACKSMITH_UPSTONE_LIST);
	pUser->Send(pAck);
}

} /*NCommand*/ } /*ProjectZ*/
